package com.walala.logic.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class ProtocolPackage implements Serializable{

	private static Logger log = LoggerFactory.getLogger(ProtocolPackage.class);

	private byte[] head;// 96字节长度
	private byte startTag; // 包开始标示 byte
	private short length; // 包头+包体总长
	private short command; // 命令字
	private long senderId; // userId
	private byte clientType; // ios/android/pc/mac....
	private byte version; // 协议版本号
	private byte result; // response时的error code
	private long stime; // 服务端时间
	private long receiverId;// 消息接受者
	private int traceId;// 系统调用跟踪id
	private byte ack;// 应答包标记；0:否、1:是

	private byte[] protoBody; // protobuf内容 byte[]
	private byte endTag; // 包结束标示 byte

	public byte[] getHead() {
		return head;
	}

	public void setHead(byte[] head) {
		this.head = head;
	}

	public byte getStartTag() {
		return startTag;
	}

	public void setStartTag(byte startTag) {
		this.startTag = startTag;
	}

	public short getLength() {
		return length;
	}

	public void setLength(short length) {
		this.length = length;
	}

	public short getCommand() {
		return command;
	}

	public void setCommand(short command) {
		this.command = command;
	}

	public long getSenderId() {
		return senderId;
	}

	public void setSenderId(long senderId) {
		this.senderId = senderId;
	}

	

	public byte getClientType() {
		return clientType;
	}

	public void setClientType(byte clientType) {
		this.clientType = clientType;
	}

	public byte getVersion() {
		return version;
	}

	public void setVersion(byte version) {
		this.version = version;
	}

	public byte getResult() {
		return result;
	}

	public void setResult(byte result) {
		this.result = result;
	}

	public long getStime() {
		return stime;
	}

	public void setStime(long stime) {
		this.stime = stime;
	}

	public long getReceiverId() {
		return receiverId;
	}

	public void setReceiverId(long receiverId) {
		this.receiverId = receiverId;
	}

	public int getTraceId() {
		return traceId;
	}

	public void setTraceId(int traceId) {
		this.traceId = traceId;
	}

	public byte getAck() {
		return ack;
	}

	public void setAck(byte ack) {
		this.ack = ack;
	}

	public byte[] getProtoBody() {
		return protoBody;
	}

	public void setProtoBody(byte[] protoBody) {
		this.protoBody = protoBody;
	}

	public byte getEndTag() {
		return endTag;
	}

	public void setEndTag(byte endTag) {
		this.endTag = endTag;
	}

	public ProtocolPackage deepCopy() {
		ByteArrayOutputStream bos = null;
		ObjectOutputStream oos = null;
		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;
		try {
			// 将该对象序列化成流,因为写在流里的是对象的一个拷贝，而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(this);
			// 将流序列化成对象
			bis = new ByteArrayInputStream(bos.toByteArray());
			ois = new ObjectInputStream(bis);

			return (ProtocolPackage) ois.readObject();
		} catch (Exception e) {
			log.error("[deepCopy]:", e);
			return null;
		} finally {
			try {
				if (bos != null) {
					bos.close();
					bos = null;
				}
				if (oos != null) {
					oos.close();
					oos = null;
				}
				if (bis != null) {
					bis.close();
					bis = null;
				}
				if (ois != null) {
					ois.close();
					ois = null;
				}
			} catch (IOException e) {
				log.error("[deepCopy close]:", e);
			}
		}
	}

}
